package com.jeesuite.admin.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

public class ConfigStateHolder {
	
	private static Map<String, ConfigState> configStates = new ConcurrentHashMap<>();
	
	public static void add(ConfigState cs){
		configStates.put(cs.getAppName() + "#" + cs.getEnv(), cs);
	}
	
	public static ConfigState get(String appName,String env){
		return configStates.get(appName + "#" + env);
	}
	
	

	public static class ConfigState{
		private String nodeId;
		private String appName;
		private String env;
		private String version;
		private Date syncTime = new Date();
		
		private Map<String, String> configs = new TreeMap<>();
		
		
		public ConfigState(Map<String, String> allMaps) {
			this.nodeId = allMaps.remove("nodeId");
			this.appName = allMaps.remove("appName");
			this.env = allMaps.remove("env");
			this.version = allMaps.remove("version");
			
			ArrayList<String> keys = new ArrayList<>(allMaps.keySet());
			Collections.sort(keys);
			for (String key : keys) {
				configs.put(key, allMaps.get(key));
			}
		}

		public String getNodeId() {
			return nodeId;
		}

		public void setNodeId(String nodeId) {
			this.nodeId = nodeId;
		}



		public String getAppName() {
			return appName;
		}
		public void setAppName(String appName) {
			this.appName = appName;
		}
		public String getEnv() {
			return env;
		}
		public void setEnv(String env) {
			this.env = env;
		}
		public String getVersion() {
			return version;
		}
		public void setVersion(String version) {
			this.version = version;
		}
		public Map<String, String> getConfigs() {
			return configs;
		}
		public void setConfigs(Map<String, String> configs) {
			this.configs = configs;
		}

		public Date getSyncTime() {
			return syncTime;
		}

		public void setSyncTime(Date syncTime) {
			this.syncTime = syncTime;
		}
		
	}
}
